﻿using BLL;
using Entity;
using Entity.FrameEntity;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json.Linq;
using YunProject.Controllers.Base;

namespace YunProject.Controllers
{
    public class WageController : BaseController
    {
        WageBLL wageBLL;
        public WageController()
        {
            wageBLL = new WageBLL();
        }
        [Authorize]
        public IActionResult WageFormulaInfo()
        {
            return View();
        }
        [Authorize]
        public IActionResult UpdateFormula()
        {
            return View();
        }
        [Authorize]
        public IActionResult GongXuFormula()
        {
            return View();
        }
        [Authorize]
        public IActionResult UpdateGxFormula()
        {
            return View();
        }
        
        [HttpPost]
        public JsonResult loadFormulaByID(string dataId)
        {
            var rsl = wageBLL.loadFormulaByID(dataId);
            return Json(rsl);
        }
        [HttpPost]
        public JsonResult DelFormulaByID(string dataId)
        {
            var rsl = new ReturnResult();
            var userObj = LoginInfo();
            if (userObj != null)
            {
                rsl = wageBLL.DelFormulaByID(dataId, userObj["userCode"].ToString());
            }
            return Json(rsl);
        }
        [HttpPost]
        public JsonResult UpdateFormula(string data)
        {
            var rsl = new ReturnResult();
            var userObj = LoginInfo();
            if (userObj != null)
            {
                rsl = wageBLL.UpdateFormula(data,userObj["baseCode"].ToString());
            }
            return Json(rsl);
        }
        [HttpPost]
        public JsonResult getFomulaInfo()
        {
            return Json(wageBLL.getFomulaInfo());
        }

        [HttpPost]
        public JsonResult SavaGongXuWage(string data)
        {
            var rsl = new ReturnResult();
            var userObj = LoginInfo();
            if (userObj != null)
            {
                rsl=wageBLL.SavaGongXuWage(data, userObj["userCode"].ToString());
            }
            return Json(rsl);
        }
        [HttpPost]
        public JsonResult loadGXFormulaByID(string dataId)
        {
            return Json(wageBLL.loadGXFormulaByID(dataId));
        }
        [HttpPost]
        public JsonResult ProductionWage(string dataId)
        {
            List<OperateModel> list = new List<OperateModel>();
            JObject obj = new JObject();
            obj["barCode"] = "000000001";
            obj["proCode"] = "0102";
            obj["number"] = "1000";//☆
            obj["createuser"] = "admin";
            obj["pUsers"] = "admin,cc";
            obj["machine"] = "m01";
            obj["hDate"] = "2024-1-1";
            string info = "";
            wageBLL.ProductionWage(obj,ref list,ref info);
            return Json(list);
        }
        
        [HttpGet]
        public JsonResult getDataList(string page, string limit, string condition, string dataType)
        {
            var rsl = new ReturnResult();
            string pageCount = "1";
            string msg = "";
            var datas = new object();
            var userObj = LoginInfo();
            if (dataType == "wsf")
            {
                rsl = wageBLL.FomulaList(page, limit, condition, ref pageCount);
            }
            else if (dataType == "wsg")
            {
                rsl = wageBLL.GongXuFomulaList(page, limit, condition, ref pageCount);
            }


            if (rsl.Result)
            {
                datas = rsl.Data;
            }
            else
            {
                msg = rsl.Message;
            }
            var dic = new Dictionary<string, object>();
            dic["count"] = pageCount;
            dic["msg"] = msg;
            dic["code"] = "0";
            dic["data"] = datas;
            return new JsonResult(dic);

        }
    }
}
